package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chexiehui.jc.R;
import com.chexiehui.jc.utils.GlideUtil;
import com.lxj.xpopup.interfaces.XPopupImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CustomXPopupImageLoader implements XPopupImageLoader {

    @Override
    public void loadImage(int position, @NonNull Object url, @NonNull ImageView imageView) {
        //必须指定Target.SIZE_ORIGINAL，否则无法拿到原图，就无法享用天衣无缝的动画
        if(url instanceof ArrayList){
            Glide.with(imageView).load(GlideUtil.getUrl(((ArrayList<String>)url).get(position)))
                    .apply(new RequestOptions()
                            .error(R.drawable.shape_square)
                            .placeholder(R.drawable.shape_square)
                            .override(Target.SIZE_ORIGINAL)).into(imageView);
        }else{
            Glide.with(imageView).load(GlideUtil.getUrl((String) url))
                    .apply(new RequestOptions()
                            .error(R.drawable.shape_square)
                            .placeholder(R.drawable.shape_square)
                            .override(Target.SIZE_ORIGINAL)).into(imageView);
        }

    }
    @Override
    public File getImageFile(@NonNull Context context, @NonNull Object uri) {
        try {
            return Glide.with(context).downloadOnly().load(uri).submit().get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
